package com.hww.common.util;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.LayoutRes;
import androidx.annotation.StyleRes;

import com.hww.common.R;

/**
 * @author hww
 * @date 0010 2024/7/10
 * email: heww@zhongyuntech.cn
 * description:
 */
public class DialogUtils {

    private DialogUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    /**
     * 根据资源ID加载布局并返回根视图。
     * <p>
     * 该方法通过LayoutInflater从指定的资源ID加载布局文件，并返回该布局的根视图。
     * 使用此方法可以方便地在不创建新类的情况下重用已有的布局资源。
     *
     * @param activity 上下文对象，用于访问系统服务和资源。
     * @param resource 布局资源的ID，通过此ID加载对应的布局文件。
     * @return 加载后的布局文件的根视图。
     */
    public static View getViewFromResource(Activity activity, @LayoutRes int resource) {
        View contentView = LayoutInflater.from(activity).inflate(resource, null);
        return contentView;
    }

    /**
     * 创建dialog弹窗(宽度方向全屏)
     *
     * @param activity    上下文对象
     * @param contentView 根视图
     * @param gravity     显示位置 {@link android.view.Gravity}   Gravity.BOTTOM or Gravity.TOP
     * @return dialog对象
     */
    public static Dialog createDialogFullScreen(Activity activity, View contentView, int gravity) {
        return createDialogFullScreen(activity, contentView, gravity, true, false, 0, 0);
    }

    /**
     * 创建dialog弹窗,默认尺寸，按照View布局进行显示
     *
     * @param activity    上下文对象
     * @param contentView 显示的View对象 {@link #getViewFromResource(Activity, int)}
     * @return 返回Dialog对象
     */
    public static Dialog createDialogDefaultScreen(Activity activity, View contentView) {
        return createDialogFullScreen(activity, contentView, Gravity.CENTER, true, false, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    /**
     * 创建dialog弹窗(宽度方向全屏)
     *
     * @param activity               上下文对象
     * @param contentView            根视图
     * @param gravity                显示位置 {@link android.view.Gravity}   Gravity.BOTTOM or Gravity.TOP
     * @param cancelable             设置对话框是否可以通过按返回键（Back键）来关闭
     * @param canceledOnTouchOutside 设置点击对话框外部区域是否关闭对话框
     * @param viewHeight             布局高度，不为0的情况下设置
     * @param viewWidth              布局宽度，为0的情况下设置屏幕的宽度，不为0的情况使用设置的宽度
     * @return dialog对象
     */
    public static Dialog createDialogFullScreen(Activity activity, View contentView, int gravity,
                                                boolean cancelable, boolean canceledOnTouchOutside, int viewHeight, int viewWidth) {
        final Dialog dialog = new Dialog(activity, R.style.CustomDialog);
        /*View contentView = LayoutInflater.from(Activity).inflate(resource, null);*/
        dialog.setContentView(contentView);
        /*ViewGroup.LayoutParams 是所有布局参数类的基类，用于描述 ViewGroup 中子视图的位置和大小*/
        ViewGroup.LayoutParams layoutParams = contentView.getLayoutParams();
        DisplayMetrics displayMetrics = activity.getResources().getDisplayMetrics();
        if (0 == viewWidth) {
            layoutParams.width = displayMetrics.widthPixels;
        } else {
            layoutParams.width = viewWidth;
        }
        if (0 != viewHeight) {
            layoutParams.height = viewHeight;
        }
        contentView.setLayoutParams(layoutParams);
        setCancelable(dialog, cancelable, canceledOnTouchOutside);
        Window window = dialog.getWindow();
        window.setGravity(gravity);
        window.setWindowAnimations(R.style.CustomDialog_Animation);
        window.setBackgroundDrawable(new ColorDrawable(0x00000000));
        return dialog;
    }

    /**
     * 创建dialog弹窗,宽度边距，高度适配
     *
     * @param activity    上下文对象
     * @param contentView 显示的View对象 {@link #getViewFromResource(Activity, int)}
     * @param gravity     显示位置 {@see android.view.Gravity}
     * @return 返回Dialog对象
     */
    public static Dialog createDialogMarginScreen(Activity activity, View contentView, int gravity) {
        return createDialogMarginScreen(activity, contentView, gravity, true, false);
    }

    /**
     * 创建dialog弹窗,宽度边距，高度适配
     *
     * @param activity               上下文对象
     * @param contentView            显示的View对象 {@link #getViewFromResource(Activity, int)}
     * @param gravity                显示位置 {@see android.view.Gravity}
     * @param cancelable             设置对话框是否可以通过按返回键（Back键）来关闭
     * @param canceledOnTouchOutside 设置点击对话框外部区域是否关闭对话框
     * @return 返回Dialog对象
     */
    public static Dialog createDialogMarginScreen(Activity activity, View contentView, int gravity, boolean cancelable, boolean canceledOnTouchOutside) {
        return createDialogMarginScreen(activity, contentView, gravity, cancelable, canceledOnTouchOutside, 8f);
    }

    /**
     * 创建dialog弹窗,宽度边距，高度适配
     *
     * @param activity               上下文对象
     * @param contentView            显示的View对象 {@link #getViewFromResource(Activity, int)}
     * @param gravity                显示位置 {@see android.view.Gravity}
     * @param cancelable             设置对话框是否可以通过按返回键（Back键）来关闭
     * @param canceledOnTouchOutside 设置点击对话框外部区域是否关闭对话框
     * @param marginSize             左右屏幕边距尺寸
     * @return 返回Dialog对象
     */
    public static Dialog createDialogMarginScreen(Activity activity, View contentView, int gravity,
                                                  boolean cancelable, boolean canceledOnTouchOutside, float marginSize) {
        return createDialogMarginScreen(activity, contentView, gravity, cancelable, canceledOnTouchOutside, marginSize, R.style.CustomDialog_Animation);
    }

    /**
     * 创建dialog弹窗,宽度边距，高度适配
     *
     * @param activity               上下文对象
     * @param contentView            显示的View对象 {@link #getViewFromResource(Activity, int)}
     * @param gravity                显示位置 {@see android.view.Gravity}
     * @param cancelable             设置对话框是否可以通过按返回键（Back键）来关闭
     * @param canceledOnTouchOutside 设置点击对话框外部区域是否关闭对话框
     * @param marginSize             左右屏幕边距尺寸
     * @param resId                  动画样式
     * @return 返回Dialog对象
     */
    public static Dialog createDialogMarginScreen(Activity activity, View contentView, int gravity,
                                                  boolean cancelable, boolean canceledOnTouchOutside,
                                                  float marginSize, @StyleRes int resId) {
        /*float marginSize = 8f;*/
        final Dialog dialog = new Dialog(activity, R.style.CustomDialog);
        dialog.setContentView(contentView);
        /*ViewGroup.MarginLayoutParams 继承自ViewGroup.LayoutParams，增加了对视图外边距（margins）的支持，用于控制视图之间的空白区域*/
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) contentView.getLayoutParams();
        params.width = activity.getResources().getDisplayMetrics().widthPixels - SizeUtils.dp2px(marginSize * 2);
        if (Gravity.BOTTOM == gravity) {
            params.bottomMargin = SizeUtils.dp2px(marginSize);
        } else if (Gravity.TOP == gravity) {
            params.topMargin = SizeUtils.dp2px(marginSize);
        }
        contentView.setLayoutParams(params);
        setCancelable(dialog, cancelable, canceledOnTouchOutside);
        Window window = dialog.getWindow();
        window.setGravity(gravity);
        window.setWindowAnimations(resId);
        window.setBackgroundDrawable(new ColorDrawable(0x00000000));
        return dialog;
    }


    /**
     * 设置对话框关闭的方式
     *
     * @param cancelable             设置对话框是否可以通过按返回键（Back键）来关闭
     * @param canceledOnTouchOutside 设置点击对话框外部区域是否关闭对话框
     */
    private static void setCancelable(Dialog dialog, boolean cancelable, boolean canceledOnTouchOutside) {
        /*设置对话框是否可以通过按返回键（Back键）来关闭  true 表示可以通过按返回键（Back键）来关闭  false 表示不可以通过按返回键（Back键）来关闭*/
        dialog.setCancelable(cancelable);
        /*设置点击对话框外部区域是否关闭对话框  true 表示点击外部区域关闭对话框  false 表示点击外部区域不关闭对话框*/
        dialog.setCanceledOnTouchOutside(canceledOnTouchOutside);
    }
}
